<script type="text/html" template lay-done="layui.data.sendParams(d);">
    <div class="layui-form coreshop-form layui-form-pane" lay-filter="LAY-app-hotbillboard-editForm" id="LAY-app-hotbillboard-editForm">

        <div class="layui-tab-item layui-show">
            <div class="layui-form-item layui-hide">
                <label for="id" class="layui-form-label  layui-form-required">编号</label>
                <div class="layui-input-block">
                    <input type="number" min="0" max="999999" name="id" lay-verType="tips" lay-verify="required|number" class="layui-input" value="{{d.params.data.id || '' }}" placeholder="请输入编号" lay-reqText="请输入编号并为数字" />
                </div>
            </div>

            <div class="layui-form-item">
                <label for="title" class="layui-form-label  layui-form-required">标题</label>
                <div class="layui-input-block">
                    <input name="title" lay-verType="tips" lay-verify="required" class="layui-input" placeholder="请输入标题" lay-reqText="请输入标题" value="{{d.params.data.title || '' }}" />
                </div>
            </div>

            <div class="layui-form-item">
                <label for="thridCategoryId" class="layui-form-label layui-form-required">第三方分类</label>
                <div class="layui-input-inline layui-inline-6">
                    <div id="thridCategoryId_SelectCreateTree" class="ew-xmselect-tree" lay-reqText="请选择第三方分类"></div>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <button href="javascript:void(0);" id="add-class1" class="add-class layui-btn layui-btn-normal layui-btn-xs">添加分类</button>
                </div>

            </div>

            <div class="layui-form-item">
                <label for="categoryId" class="layui-form-label layui-form-required">商品分类</label>
                <div class="layui-input-inline layui-inline-6">
                    <div id="categoryId_SelectCreateTree" class="ew-xmselect-tree" lay-reqText="请选择商品分类"></div>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <button href="javascript:void(0);" id="add-class2" class="add-class layui-btn layui-btn-normal layui-btn-xs">添加分类</button>
                </div>

            </div>



            <div class="layui-form-item">

                <!--<fieldset class="layui-elem-field layui-field-title site-title"><legend><a name="compress">封面图</a></legend></fieldset>-->

                <div class="layui-form-item">
                    <label class="layui-form-label  layui-form-required" for="avatarImage">封面图：</label>
                    <div class="layui-input-inline layui-inline-10">
                        <input name="image" id="fengmianImage" size="15" lay-verify="required" autocomplete="off" class="layui-input" lay-reqText="请输入【缩略图】" value="{{d.params.data.image || '' }}" />
                    </div>
                    <div class="layui-input-inline">
                        <div class="layui-upload">
                            <button type="button" class="layui-btn" id="upBtn">上传图片</button>
                            <div class="layui-upload-list">
                                <img class="layui-upload-img" id="viewImgBox" src="{{ d.params.data.image }}">
                                <p id="viewTextBox"></p>
                            </div>
                        </div>
                    </div>
                </div>

                <!--<fieldset class="layui-elem-field layui-field-title site-title"><legend><a name="compress">图集</a></legend></fieldset>-->
                <!--<div class="layui-form-item">
                <div class="layui-input-inline layui-inline-2">
                    <input type="hidden" name="images" id="imageIds" class="layui-input" placeholder="请上传缩略图" lay-reqText="请上传缩略图" />
                    <button type="button" class="layui-btn  layui-btn-sm" id="upBtnimageIds"><i class="layui-icon"></i>上传图片</button>
                </div>
                <div class="layui-form-mid">
                    图集：[建议数量控制在10张以内]：
                </div>
            </div>
            <div class="layui-form-item">
                <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                    <div class="layui-upload-list" id="upload-more-list"></div>
                </blockquote>
            </div>-->


            </div>

        </div>


        <!--<div class="layui-tab layui-tab-card" style="margin: 0 0 10px 0">
        <ul class="layui-tab-title">
            <li class="layui-this">基础信息</li>
            <li>-->
        <!--排行榜信息-->
        <!--</li>
    </ul>
    <div class="layui-tab-content" style="min-height: 500px;">-->
        <!--基础编辑-->
        <!-- 产品tab -->
        <!--<div class="layui-tab-item">
                <div id="LAY-app-tablethrid-toolbar">
                    <div class="layui-form coreshop-toolbar-search-form">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <div class="layui-input-inline">
                                    <input type="text" name="searchuserphone" id="searchuserphone" placeholder="请输入手机号码" class="layui-input">
                                </div>
                            </div>

                            <div class="layui-inline">

                                <div class="layui-inline">
                                    <button class="layui-btn layui-btn-sm" lay-submit lay-filter="LAY-app-CoreCmsGoodsUser-search"><i class="layui-icon layui-icon-search"></i>查找并添加</button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <table id="tablethrid" lay-filter="tablethridevent">
                    </table>
                </div>
            </div>
        </div>

    </div>-->


        <div class="layui-form-item text-right core-hidden">
            <input type="button" class="layui-btn" lay-submit lay-filter="LAY-app-hotbillboard-editForm-submit" id="LAY-app-hotbillboard-editForm-submit" value="确认编辑">
        </div>
    </div>
</script>

<script id="image_tpl" type="text/html">
    {{# layui.each(d, function(index, item){  }}
    <div class="imgdiv">
        <img src="{{ item }}" class="layui-upload-img" onclick='layui.coreHelper.viewImage("{{ item }}")' style="width: 100px;height:100px;">
        <div id="operate">
            <div><a class="del" onclick="delImg(this,'{{ item }}')">删除</a>|<a class="setmain" onclick="setDefault(this,'{{ item }}')">设为主图</a></div>
        </div>
    </div>
    {{#  }); }}
</script>

<script>
    var debug= layui.setter.debug;
    layui.data.sendParams = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d.params.data); }
        layui.use(['admin', 'form', 'view', 'laydate', 'upload', 'coreHelper', 'cropperImg', "laytpl",'xmSelect'],
        function () {
            var $ = layui.$
                , form = layui.form
                , admin = layui.admin
                , laytpl = layui.laytpl
                , laydate = layui.laydate
                , upload = layui.upload
                , view = layui.view
                , cropperImg = layui.cropperImg
                , xmSelect = layui.xmSelect
                , coreHelper = layui.coreHelper;
        
            laydate.render({
                elem: '#editTime-hotbillboard-creattime',
                type: 'datetime'
            });
            form.verify({
                
            });

            //数据绑定-用户
            //table.render({
            //    elem: '#tablethrid',
            //    url: layui.setter.apiUrl + 'Api/HotbillboardThrid/GetPageList?meetingid=' + d.params.data.id,
            //    method: 'POST',
            //    //pagebar: '#LAY-app-CoreCmsGoods-pagebar',
            //    /*   toolbar: '#LAY-app-CoreCmsGoods-toolbar',*/
            //    className: 'pagebarbox',
            //    defaultToolbar: [],
            //    height: 'full-179',//无面包屑127,搜索框189,1行62
            //    page: true,
            //    limit: 30,
            //    limits: [10, 15, 20, 25, 30, 50, 100, 200],
            //    text: { none: '暂无相关数据' },
            //    cols: [
            //        [
            //            /*{ type: "checkbox", fixed: "left" },*/
            //            //{ width: 142, align: 'center', title: '操作', toolbar: '#LAY-app-MeetingGoodsUser-tableBox-bar' },
            //            { field: 'phone', title: '当前状态', width: 130, sort: false },
            //            { field: 'isAllow', title: '访问', width: 80, templet: '#switch_isAllow', sort: false, unresize: true },

            //            { field: 'phone', title: '用户手机号', width: 130, sort: false },
            //            { field: 'addtime', title: '添加时间', width: 130, sort: false },
            //            { field: 'fromType', title: '来源', width: 130, sort: false },




            //        ]
            //    ]

            //});

            //获取三方分类
            coreHelper.Get("Api/HotbillBoard/GetThirdCategory", function (e) {

                //table.reloadData('LAY-app-CoreCmsGoods-tableBox');

                // 渲染商品分类
                var insXmSelThridCategoryId = xmSelect.render({
                    el: '#thridCategoryId_SelectCreateTree',
                    height: '150px',
                    data: e.data,
                    initValue: d.params.data ? [d.params.data.thridCategoryId] : [],
                    name: 'thridCategoryId',
                    prop: {
                        name: 'categoryName',
                        value: 'id'
                    },
                    //layVerify: 'verifyGoodsCategory',
                    //layVerType: 'msg',
                    radio: true,
                    clickClose: true,
                    tree: {
                        show: true,
                        indent: 15,
                        strict: false,
                        expandedKeys: true
                    }
                });

                //添加分类
                $(document).on('click', '#add-class1', function () {
                    admin.popup({
                        shadeClose: false,
                        title: '添加第三方分类',
                        area: ['700px', '540px'],
                        id: 'LAY-app-hotbillboardthridcategory-createForm',
                        success: function (layero, index) {
                            view(this.id).render('hotbillboard/category/create', {}).done(function () {
                                //监听提交

                                form.on('submit(LAY-app-hotbillboardthridcategory-createForm-submit)',
                                    function (data) {
                                        var field = data.field; //获取提交的字段
                                        field.isShow = field.isShow == 'on';
                                        if (debug) { console.log(field); } //开启调试返回数据
                                        //提交 Ajax 成功后，关闭当前弹层并重载表格
                                        coreHelper.Post("Api/HotbillBoard/DoCreateThirCate", field, function (e) {
                                            if (e.code === 0) {
                                                //重新渲染分类下拉
                                                insXmSelThridCategoryId.update({ data: e.data, autoRow: true, })

                                                form.render();
                                                layer.close(index); //再执行关闭
                                                layer.msg(e.msg);
                                            } else {
                                                layer.msg(e.msg);
                                            }
                                        });
                                    });
                            });
                        }
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            layero.contents().find("#LAY-app-hotbillboardthridcategory-createForm-submit").click();
                        }
                    });
                });


                ///
                form.render();

            });

            //获取商品分类
            coreHelper.Post("Api/CoreCmsGoods/GetIndex", {}, function (e) {
               

                // 渲染商品分类
                var insXmSelGoodsCategoryId = xmSelect.render({
                    el: '#categoryId_SelectCreateTree',
                    height: '150px',
                    data: e.data.categories.data,
                    initValue: d.params.data ? [d.params.data.categoryId] : [],
                    //initValue: mData ? [mData.parentId] : [],
                    //model: { label: { type: 'text' } },
                    name: 'categoryId',
                    prop: {
                        name: 'title',
                        value: 'id'
                    },
                    //layVerify: 'verifyGoodsCategory',
                    //layVerType: 'msg',
                    radio: true,
                    clickClose: true,
                    tree: {
                        show: true,
                        indent: 15,
                        strict: false,
                        expandedKeys: true
                    }
                });

                //添加分类
                $(document).on('click', '#add-class2', function () {
                    coreHelper.Post("Api/CoreCmsGoodsCategory/GetCreate", null, function (e) {
                        if (e.code === 0) {
                            admin.popup({
                                shadeClose: false,
                                title: '添加分类',
                                area: ['700px', '540px'],
                                id: 'LAY-popup-CoreCmsGoodsCategory-create',
                                success: function (layero, index) {
                                    view(this.id).render('good/goodscategory/create', { data: e.data }).done(function () {
                                        //监听提交
                                        form.on('submit(LAY-app-CoreCmsGoodsCategory-createForm-submit)',
                                            function (data) {
                                                var field = data.field; //获取提交的字段
                                                field.isShow = field.isShow == 'on';
                                                if (debug) { console.log(field); } //开启调试返回数据
                                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                                coreHelper.Post("Api/CoreCmsGoodsCategory/DoCreate", field, function (e) {
                                                    if (e.code === 0) {
                                                        //重新渲染分类下拉
                                                        insXmSelGoodsCategoryId.update({ data: e.data.categories.data, autoRow: true, })

                                                        form.render();
                                                        layer.close(index); //再执行关闭
                                                        layer.msg(e.msg);
                                                    } else {
                                                        layer.msg(e.msg);
                                                    }
                                                });
                                            });
                                    });
                                }
                                , btn: ['确定', '取消']
                                , yes: function (index, layero) {
                                    layero.contents().find("#LAY-app-CoreCmsGoodsCategory-createForm-submit").click();
                                }
                            });
                        } else {
                            layer.msg(e.msg);
                        }
                    });
                });



                ///
                form.render();

            });




            //初始化图片
            if (d.params.data.images) {
                imgs = d.params.data.images.split(',');
                defaultImg = imgs[0];
                $('#imageIds').val(imgs);
                var getTpl = image_tpl.innerHTML, viewBox = document.getElementById('upload-more-list');
                laytpl(getTpl).render(imgs, function (html) {
                    viewBox.innerHTML = html;
                });
            }
            //封面图图片上传
            $('#upBtn').click(function () {
                cropperImg.cropImg({
                    aspectRatio: 1 / 1,
                    imgSrc: $('#viewImgBox').attr('src'),
                    onCrop: function (data) {
                        var loadIndex = layer.load(2);
                        coreHelper.Post("api/Tools/UploadFilesFByBase64", { base64: data }, function (res) {
                            if (0 === res.code) {
                                $('#viewImgBox').attr('src', res.data.fileUrl);
                                $("#fengmianImage").val(res.data.fileUrl);
                                layer.msg(res.msg);
                                layer.close(loadIndex);
                            } else {
                                layer.close(loadIndex);
                                layer.msg(res.msg, { icon: 2, anim: 6 });
                            }
                        });
                    }
                });
                form.render();
            });

            //图集上传
            $('#upBtnimageIds').click(function () {
                cropperImg.cropImg({
                    aspectRatio: 1 / 1,
                    onCrop: function (data) {
                        var loadIndex = layer.load(2);
                        coreHelper.Post("api/Tools/UploadFilesFByBase64", { base64: data }, function (res) {
                            if (0 === res.code) {
                                imgs.push(res.data.fileUrl);
                                $('#imageIds').val(imgs);
                                var getTpl = image_tpl.innerHTML, view = document.getElementById('upload-more-list');
                                laytpl(getTpl).render(imgs, function (html) {
                                    view.innerHTML = html;
                                });
                                layer.msg(res.msg);
                                layer.close(loadIndex);
                            } else {
                                layer.close(loadIndex);
                                layer.msg(res.msg, { icon: 2, anim: 6 });
                            }
                        });
                    }
                });
                form.render();
            });


            //重载form
            form.render(null, 'LAY-app-hotbillboard-editForm');
        })
    };

    //删除图片
    function delImg(obj, imageUrl) {
        var imgDiv = $(obj).parent().parent().parent();
        imgDiv.remove();
        var index = imgs.indexOf(imageUrl);
        if (index > -1) {
            imgs.splice(index, 1);
        }
        $('#imageIds').val(imgs);
    }
    //设为默认图
    function setDefault(obj, imageUrl) {
        var imgDiv = $(obj).parent().parent().parent();
        $("#upload-more-list").prepend(imgDiv);
        var index = imgs.indexOf(imageUrl);
        if (index > -1) {
            imgs.unshift(imgs.splice(index, 1)[0]);
        }
        $('#imageIds').val(imgs);
    }
</script>
